package org.meetunexpectedly.service.file.impl;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.meetunexpectedly.config.QiNiuConfig;
import org.meetunexpectedly.service.file.QiNiuFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 七牛云oss服务实现接口
 * @Author: pupil
 * @Date: 2024/08/17 下午 7:19
 */
@Service
public class QiNiuFileServiceImpl implements QiNiuFileService {

    @Autowired
    private QiNiuConfig qiNiuConfig;

    @Override
    public String getToken() {
        return qiNiuConfig.videoUploadToken();
    }

    @Override
    public FileInfo getFileInfo(String fileKey) {
        Configuration cfg = new Configuration(qiNiuConfig.getRegion());
        Auth auth = qiNiuConfig.buildAuth();
        String bucketName = qiNiuConfig.getBucketName();

        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            // 获取存储空间中文件的属性   空间名称，文件名称
            FileInfo fileInfo = bucketManager.stat(bucketName, fileKey);
            return fileInfo;
        } catch (QiniuException e) {
            e.printStackTrace();
            System.err.println(e.response.toString());
        }
        return null;
    }
}
